//-----------------这是页面内公共部分js效果的js代码---------------------------
//------------------站内搜索框--------------------
function actionSerach(){
	var oSerach=document.getElementById("serach");
	var oSerachBanner=document.getElementById("serach-banner");
	var oSerachBox=document.getElementById("serach-box");
	var oClose=oSerachBox.getElementsByTagName("span")[0];
	var turn=true;
	oSerach.onclick=function(){
		if(turn){
			sliddown("serach-banner",80,function(){
				oSerachBox.style.display="block";
			})
		}else{
			oSerachBox.style.display="none";
			sliddown("serach-banner",0)
		}
		turn=!turn;
		oSerachBox.onmouseover=oClose.onmouseover=oSerachBanner.onmouseover=function(){	
			oClose.style.display="block";
			oClose.onclick=function(){
				oSerachBox.style.display="none";
				sliddown("serach-banner",0)
				turn=!turn;
			}
			oSerachBanner.onmouseout=function(){
				oClose.style.display="none";
			}
		}
	}
}

function actionBrandSeries(){
	var oBox=document.getElementById("brand-series");
	var oBrand=document.getElementById("brand");
	oBox.onmouseover=function(){
		var iHeight=parseInt(document.documentElement.clientHeight)||parseInt(document.body.clientHeight);
		startMove(10,oBrand,{width:74,height:iHeight,opacity:100})
	}
	oBox.onmouseout=function(){
		startMove(10,oBrand,{width:0,height:0,opacity:0})
	}
}
function actionKefu(){
	var oParent=document.getElementById("kefu");
	var oChild=document.getElementById("kefu-number");
	oParent.onmouseover=function(){
		oChild.style.display="block";
		startMove(10,oChild,{left:-231,opacity:100})
	}
	oParent.onmouseout=function(){
		startMove(10,oChild,{left:-300,opacity:0},function(){
			oChild.style.display="none";
		})
	}
}

function actionScroll(){//滚动事件的函数
	var oGoTop=document.getElementById("gotop");
	var oCover1=$("#cover1");
	var oCover2=$("#cover2");
	var turn=true;
	window.onscroll=function(){
		var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;
		oCover1.style.top=oCover2.style.top=scrolltop+"px";
		if(scrolltop>=100&&turn==true){
			turn=false;
			oGoTop.style.display="block";
			startMove(15,oGoTop,{opacity:100});
		}
		if(scrolltop<100&&turn==false){
			turn=true;
			startMove(15,oGoTop,{opacity:0},function(){
				oGoTop.style.display="none";
			});
		}
	}
	oGoTop.onclick=function(){
		document.documentElement.scrollTop=document.body.scrollTop=0;
	}
}

function actionTip(text){
	var oCover=$("#cover1");
	var oTip=$("#tip");
	var oClose=$(".close",oTip)[0];
	var oBtn=$("#tip-btn");
	var oText=$("p",oTip)[0];
	if(text){
		str=text;
	}else{
		str="您选择的商品已经添加进购物车！";
	}
	oText.innerHTML=str;
	var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;
	oCover.style.top=scrolltop+"px";
	oCover.style.display='block';
	oBtn.onclick=oClose.onclick=function(){
		oTip.style.top="-210px";
		oCover.style.display="none";
		oCover.style.opacity="0";
		oCover.style.filter="alpha(opacity=0)";
	}
	startMove(8,oCover,{opacity:60},function(){
		startMove(8,oTip,{top:(scrolltop+300)})
	})
}

//---------检测用户名密码函数------------

function Register(key,value){
	var str=getCookie(key);
	if(str){
		var arr=str.split("?");
	}else{
		var arr=[];
	}
	
	for(var i=0;i<arr.length;i++){
		if(arr[i]==value){
			return true;
			break;	
		}
	}
	if(i==arr.length){
		return false;
	}
}

//---------------检测登录状态-------------------

function Testsign(){
	var remember=getCookie("MCremember");
	var username=document.getElementById("mcusername");
	var zhuce=document.getElementById("mczhuce")
	if(remember){//如果自动登录状态存在，则开启登录状态，登录状态码为记忆的$password
		setCookie("status",remember,1,'/');
	}
	var status=getCookie("status");
	if(status){
		testgouwuche();
		var arr=status.split("-");
		username.innerHTML=arr[0];
		zhuce.innerHTML="退出";	
		zhuce.onclick=function(){//点退出后，清除登录状态以及自动登录状态
			removeCookie("MCremember");
			removeCookie("status");
			window.location.reload();
			return false;
		}
	}
	window.onbeforeunload=function(){
		removeCookie("status");
	}
	return false;
}
//-----------检测购物车数量函数----------------
function testgouwuche(){
	var Num=document.getElementById("gouwuche-number");
	var num=getGoodsCode().length;
	Num.innerHTML=num;
}
//-------------------检测登录用户的购物车商品编号------------------------------
function getGoodsCode(){//必须在登录环境下执行
	var status=getCookie("status");
	var aa=status+"gouwuche"
	var str;
	var arr=[];
	if(getCookie(aa)==undefined){
		arr=[];
	}else{
		str=getCookie(aa);//获取statusgouwuche中存的商品码
		arr=str.split("?");
	}
	return arr;
}
//-----------------获取购物车数据(不重复,获取重复的数量)-------------------------------------
function getdata(){
	var data1=[];
	var arr=data.goods;
	var code=getGoodsCode();
	if(code){
		for(var i=0;i<arr.length;i++){
			arr[i].num=1;
			for(var j=0;j<code.length;j++){
				if(code[j]==arr[i].goodscode){
					var turn=true
					for(var k=0;k<data1.length;k++){
						if(data1[k]==arr[i]){//判断是否存在一样的数，若存在，则关闭开关，将该对象的num值加1
							data1[k].num++;
							turn=false;
							break;
						}
					}
					if(turn){
						data1.push(arr[i]);
					}
				}
			}
		}
	}	
	return data1;
}
//-----------购物车数据（重复）----------------
function getGooddatas(){
	var data1=[];
	var arr=data.goods;
	var code=getGoodsCode();
	if(code){
		for(var i=0;i<arr.length;i++){
			for(var j=0;j<code.length;j++){
				if(code[j]==arr[i].goodscode){
					data1.push(arr[i]);
				}
			}
		}
	}	
	return data1;
}

//-------------------添加进购物车-----------------------------------
function togouwuche(code){
	var status=getCookie("status");
	if(status){
		var gouwuche=getCookie(status+"gouwuche");
		if(gouwuche){
			setCookie(status+"gouwuche",gouwuche+"?"+code,3,"/");
		}else{
			setCookie(status+"gouwuche",code,3,"/");
		}
		testgouwuche();
		actionTip("您选的商品已添加进购物车");
		console.log(getGoodsCode())
	}else{
		actionTip("请先登录");
	}
}
function togouwu(code){
	var status=getCookie("status");
	if(status){
		var gouwuche=getCookie(status+"gouwuche");
		if(gouwuche){
			setCookie(status+"gouwuche",gouwuche+"?"+code,3,"/");
		}else{
			setCookie(status+"gouwuche",code,3,"/");
		}
		testgouwuche();
	}
}

function serach(){
	var oSerach=$("#serach-box");
	var oInput=$("input",oSerach)[0];
	var oBtn=$("button",oSerach)[0];
	var arr=data.goods;
	var str=""
	oBtn.onclick=function(){
		var re=new RegExp(oInput.value,"g");
		for(var i=0;i<arr.length;i++){
			if(re.test(arr[i].title)){
				if(str==""){
					str=arr[i].goodscode
				}else{
					str=str+"?"+arr[i].goodscode
				}
			}
		}
		setCookie("serach",str,3,"/");
		window.open("serach.html");
		return false;
	}
}
function serach1(){
	var oSerach=$("#serach-box");
	var oInput=$("input",oSerach)[0];
	var oBtn=$("button",oSerach)[0];
	var arr=data.goods;
	var str=""
	oBtn.onclick=function(){
		var re=new RegExp(oInput.value,"g");
		for(var i=0;i<arr.length;i++){
			if(re.test(arr[i].title)){
				if(str==""){
					str=arr[i].goodscode
				}else{
					str=str+"?"+arr[i].goodscode
				}
			}
		}
		setCookie("serach",str,3,"/");
		window.open("html/serach.html");
		return false;
	}
}
function getserach(){
	var data1=[];
	var arr=data.goods;
	var code=serachGoodsCode();
	if(code){
		for(var i=0;i<arr.length;i++){
			for(var j=0;j<code.length;j++){
				if(code[j]==arr[i].goodscode){
					data1.push(arr[i]);
				}
			}
		}
	}	
	return data1;
}
function serachGoodsCode(){//必须在登录环境下执行
	var str;
	var arr=[];
	if(getCookie("serach")==undefined){
		arr=[];
	}else{
		str=getCookie("serach");//获取statusgouwuche中存的商品码
		arr=str.split("?");
	}
	return arr;
}

